package com.dy.placeOrder2.controller;



import com.dy.placeOrder.Shoppingcart;
import com.dy.placeOrder2.service.ShoppingcartService;
import com.kaoshi.common.config.Result;
import com.dy.pojo.Goods;
import com.dy.pojo.Orders;
import com.dy.placeOrder2.service.GoodsService;
import com.dy.placeOrder2.service.OrdersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService orderService;

    @Autowired
    private GoodsService goodsService;


    //购买商品，把商品信息和规格存到订单表，库存减一，销量加一。
    @PostMapping("/add")
    public Result add(@RequestBody Orders orders) {
         Integer id=orders.getId();
        Goods goods = goodsService.find(id);
        goods.setInventory(goods.getInventory()-orders.getSl());
        goods.setSales(goods.getSales()+orders.getSl());
        orders.setGid(goods.getId());
        orders.setName(goods.getName());
        orders.setSf(goods.getProvince());
        orders.setYd(goods.getExpressage());
        orders.setXl(goods.getSales());
        orders.setFw(goods.getServe());
        orders.setTp(goods.getImg());
        orders.setFhsj(goods.getShipments());
        orders.setYj(goods.getScore1());
        orders.setKc(goods.getInventory());
        orders.setBq(goods.getType());
        orders.setSc(goods.getCollect());
        orders.setSellerid(goods.getUid());
        orders.setZt(0);
        orders.setYf(goods.getPostage());
        orders.setGstatus(0);
        goodsService.updateById(goods);
        orderService.save(orders);
        Integer id1 = orders.getId();
        return Result.ok(id1);
    }


    @PostMapping("/AddGwc")
    public Result AddGwc(@RequestBody Orders orders) {
        Integer id=orders.getId();
        Goods goods = goodsService.find(id);
        goods.setInventory(goods.getInventory()-orders.getSl());
        goods.setSales(goods.getSales()+orders.getSl());
        orders.setGid(goods.getId());
        orders.setName(goods.getName());
        orders.setSf(goods.getProvince());
        orders.setYd(goods.getExpressage());
        orders.setXl(goods.getSales());
        orders.setFw(goods.getServe());
        orders.setTp(goods.getImg());
        orders.setFhsj(goods.getShipments());
        orders.setYj(goods.getScore1());
        orders.setKc(goods.getInventory());
        orders.setBq(goods.getType());
        orders.setSellerid(goods.getUid());
        orders.setSc(goods.getCollect());
        orders.setZt(0);
        orders.setYf(goods.getPostage());
        orders.setGstatus(1);
        goodsService.updateById(goods);
        orderService.save(orders);


        return Result.ok();
    }


}
